package com.nineclock.attendance.service.impl;
import com.nineclock.common.utils.DateTimeUtil;
import lombok.Data;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nineclock.attendance.dto.AtHolidayRuleDto;
import com.nineclock.attendance.mapper.AtHolidayRuleMapper;
import com.nineclock.attendance.pojo.AtHolidayRule;
import com.nineclock.attendance.service.AtHolidayRuleService;
import com.nineclock.common.entity.PageResult;
import com.nineclock.common.filter.CurrentUserHolder;
import com.nineclock.common.utils.BeanHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;

@Service
@Transactional
public class AtHolidayRuleServiceImpl implements AtHolidayRuleService {
    @Autowired(required = false)
    private AtHolidayRuleMapper atHolidayRuleMapper;
    /**
     * 假期管理*/
    @Override
    public PageResult<AtHolidayRuleDto> selectHoliday(Integer page, Integer pageSize) {
        //设置页面大小
        IPage<AtHolidayRule> iPage=new Page<>(page,pageSize);
        //查询
        LambdaQueryWrapper<AtHolidayRule> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(AtHolidayRule::getCompanyId, CurrentUserHolder.get().getCompanyId());
        iPage = atHolidayRuleMapper.selectPage(iPage, wrapper);
        //首页
        List<AtHolidayRule> atHolidayRules = iPage.getRecords();
        if (!CollectionUtils.isEmpty(atHolidayRules)){
            List<AtHolidayRuleDto> atHolidayRuleDtos = BeanHelper.copyWithCollection(atHolidayRules, AtHolidayRuleDto.class);
            return new PageResult<AtHolidayRuleDto>(iPage.getTotal(), iPage.getPages(), atHolidayRuleDtos);
        }
        return new PageResult<AtHolidayRuleDto>(iPage.getTotal(), iPage.getPages(), null);
    }
    /**
     * 假期删除*/
    @Override
    public void deleteById(Long id) {
        atHolidayRuleMapper.deleteById(id);
    }




    /**
     * 修改假期*/

    @Override
    public void updateHoliday(Long id, AtHolidayRuleDto atHolidayRuleDto) {
        AtHolidayRule atHolidayRule = BeanHelper.copyProperties(atHolidayRuleDto, AtHolidayRule.class);
        atHolidayRule.setId(id);
         atHolidayRuleMapper.updateById(atHolidayRule);
    }



    /**
     * 新增假期*/
    @Override
    public void addHoliday(AtHolidayRuleDto atHolidayRuleDto) {
        //校验
        if(atHolidayRuleDto!=null){
            //数据转换
            AtHolidayRule atHolidayRule = BeanHelper.copyProperties(atHolidayRuleDto, AtHolidayRule.class);
            atHolidayRule.setUpdateTime(DateTimeUtil.dateToStr(new Date()));
            atHolidayRule.setCompanyId(CurrentUserHolder.get().getCompanyId());
            atHolidayRule.setCreateTime(DateTimeUtil.dateToStr(new Date()));
            List<Integer> ruleByServiceTime = atHolidayRuleDto.getRuleByServiceTime();
            //数据库String类型，进行拼接
            String str ="";
            for (Integer ruleStr : ruleByServiceTime) {
                str+= ruleStr+",";
            }
            atHolidayRule.setRuleByServiceTime(str.substring(0,str.length()-1));
            //返回
            atHolidayRuleMapper.insert(atHolidayRule);
        }
    }
}
